package com.southpark.demo.view.pages.home
{
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.southpark.demo.control.events.data.GetDataEvent;
	import com.southpark.demo.control.events.data.SetDataEvent;
	import com.southpark.demo.control.events.info.AddInfoEvent;
	import com.southpark.demo.control.events.navigation.AddPageEvent;
	import com.southpark.demo.control.events.navigation.AddPopUpEvent;
	import com.southpark.demo.control.events.user.UserLoggedInEvent;
	import com.southpark.demo.control.events.user.UserLoggedOutEvent;
	import com.southpark.demo.interfaces.pages.IHomePage;
	import com.southpark.demo.interfaces.pages.IWelcomePage;
	import com.southpark.demo.interfaces.popUps.IUserLoginPopUp;
	import com.southpark.demo.view.BaseViewController;
	import com.southpark.demo.view.pages.home.interfaces.IHomePageCtrl;
	import com.southpark.demo.view.pages.home.interfaces.IHomePageUI;
	import com.southpark.framework.control.responders.IResponder;
	import com.southpark.framework.control.responders.ResponderFactory;
	
	import mx.collections.ArrayCollection;
	
	
	public class HomePageCtrl extends BaseViewController implements IHomePage,IHomePageCtrl
	{		
		
		public function HomePageCtrl(view:IHomePageUI)
		{
			super();
			this.view = view;
		}
		
		
		/**
		 * IViewController
		 */
		override public function onCreated():void
		{
			intialize();
		}
		
		override public function onShow():void
		{
			
		}
		
		override public function onHide():void
		{
			
		}
		
		
		/**
		 * IHomePage
		 */
		

		
		
		/**
		 * IHomePageCtrl
		 */
		private var view:IHomePageUI;
		
		
		private var _backgroundColor:uint;
		[Bindable]public function get backgroundColor():uint {return _backgroundColor};
		public function set backgroundColor(value:uint):void {_backgroundColor = value};
		
		private var _backgroundText:String;
		[Bindable]public function get backgroundText():String {return _backgroundText};
		public function set backgroundText(value:String):void {_backgroundText = value};
		
		
		private function intialize():void
		{
			applicationManager.addEventListener(UserLoggedInEvent.USER_LOGGED_IN_EVENT,onUserLoggedIn);
			applicationManager.addEventListener(UserLoggedOutEvent.USER_LOGGED_OUT_EVENT,onUserLoggedOut);
			
			manageSession();
		}

		private function onUserLoggedIn(event:CairngormEvent):void
		{
			manageSession();
		}
		
		private function onUserLoggedOut(event:CairngormEvent):void
		{
			manageSession();
		}
		
		private function manageSession():void
		{	
			if (model.sessionId)
			{
				backgroundColor = 0xFFFF00;
				backgroundText = applicationManager.textManager.getText("XRT_001");
			}
			else
			{
				backgroundColor = 0xFFFFFF;
				backgroundText = applicationManager.textManager.getText("XRT_002");
			};
		}
		
		
		/**
		 * 
		 */
		public function userLogin():void
		{
			var e:AddPopUpEvent = new AddPopUpEvent(AddPopUpEvent.ADD_POP_UP_EVENT);
			e.popUpInterface = IUserLoginPopUp;
			e.dispatch();
		}
		
		public function welcomePage():void
		{
			var e:AddPageEvent = new AddPageEvent(AddPageEvent.ADD_PAGE_EVENT);
			e.pageInterface = IWelcomePage;
			e.dispatch();
		}
		
		
		/**
		 * 
		 */
		private var _dataList:ArrayCollection;
		[Bindable]public function get dataList():ArrayCollection {return _dataList};
		public function set dataList(value:ArrayCollection):void {_dataList = value};
		
		public function getData1():void
		{
			var responder:IResponder = ResponderFactory.createResponder(onGetDataSuccess);
			var e:GetDataEvent = new GetDataEvent(GetDataEvent.GET_DATA_1_EVENT,responder);
			e.param1 = "param1";
			e.param2 = "param2";
			e.dispatch();
		}
		
		public function getData2():void
		{
			var responder:IResponder = ResponderFactory.createResponder(onGetDataSuccess);
			var e:GetDataEvent = new GetDataEvent(GetDataEvent.GET_DATA_2_EVENT,responder);
			e.param1 = "param1";
			e.param2 = "param2";
			e.dispatch();
		}
		
		private function onGetDataSuccess(result:Object):void
		{
			dataList = result as ArrayCollection;
		}
		
		/**
		 * 
		 */
		public function setData():void
		{
			var responder:IResponder = ResponderFactory.createResponder(onSetDataSuccess);
			var e:SetDataEvent = new SetDataEvent(SetDataEvent.SET_DATA_EVENT,responder);
			e.param1 = "param1";
			e.param2 = "param2";
			e.dispatch();
		}
		
		private function onSetDataSuccess(result:Object):void
		{
			dataList = result as ArrayCollection;
			
			var e:AddInfoEvent = new AddInfoEvent(AddInfoEvent.ADD_CONFIRMATION);
			e.text = "data saved";
			e.dispatch();
		}

		
	}
}